<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/1
 * Time: 13:31
 */

namespace Home\Controller;
class CorpsController extends  CommonController
{
    public function index()
    {

        header("Content-type:text/html;charset=utf-8");
        $loginuser = $this->_user(true);

        //战队信息
        $this->assign('corps',$loginuser);
        //参与场次
        $Team  = D('Team');
        $corpsnumber = $loginuser['corpsnumber'];
        $data = $Team->where("corpsnumbers = '".$corpsnumber."'")->select();
        if(empty($data)){

        }
        $this->assign('data',$data);
        $defeat=0;
        $win=0;

        for($i=0;$i<count($data);$i++){
            if($data[$i]['result']=='胜'){  //统计胜利数量
                $win++;
            }
            if($data[$i]['result']=='负'){  //统计失败数量
                $defeat++;
            }
            $matchs_ids[$i]=$data[$i]['match_id'];

        }
        //胜率
        if($defeat==0){
            $win_rate = 100;
        }
        else if($win==0){
            $win_rate = 0;
        }
        else{
            $win_rate  = round($win/round(count($data)),2)*100;  //计算胜率
        }

        //胜率
        $this->assign("win_rate",$win_rate);

        //团队成员
        $members = D('Members')->where("lolnumbers='".$loginuser['corpsnumber']."'")->select();
        $this->assign('members',$members);

        $matchs_str = implode(",",$matchs_ids);
        $matchs = D('Match')->where("id in($matchs_str)")->Order("match_date desc")->field("id,name,match_date,
        (SELECT result FROM lol_team WHERE lol_team.match_id = lol_match.id AND corpsnumbers = '$corpsnumber') result,
        ( SELECT id FROM lol_team WHERE lol_team.match_id = lol_match.id AND corpsnumbers = '$corpsnumber' ) team_id")->Order("created_at desc")->select();

        for($i=0;$i<count($matchs);$i++){
            $match_id = $matchs[$i]['id'];
            $matchs[$i]['team']= $Team->where("match_id=$match_id")->field("name,camp")->Order("match_id desc,camp desc")->select();
        }

        $this->assign("matchs",$matchs);
        $this->display();
    }
    public function  detail($id){
        header("Content-type:text/html;charset=utf-8");
        $loginuser = $this->_user(true);
        $Team = D('Team');
        //查询两个比赛队伍信息
        $teams = $Team
            ->join("lol_match on lol_team.match_id = lol_match.id")
            ->where("lol_match.id=(SELECT match_id FROM `lol_team` where id=$id)")->Order("camp desc")
            ->field("lol_match.name match_name,match_date,when_long,match_type,mvp,lol_team.name team_name,
            result,big_dragon,small_dragon,horizons,date_s,lol_team.id team_id")->select();
        $Data = D('Data');
        for($i=0;$i<count($teams);$i++){
            $team_id = $teams[$i]['team_id'];
            $teams[$i]['kill_num']=$Data->where("team_id=$team_id")->sum("`kill`");
            $teams[$i]['money']=round($Data->where("team_id=$team_id")->sum("`money`"),2);
        }
        if($teams[0]['result']=='胜'){
            $teams[0]['color']='z-green';
        }
        else{
            $teams[0]['color']='z-red';
        }
        if($teams[1]['result']=='胜'){
            $teams[1]['color']='z-green';
        }
        else{
            $teams[1]['color']='z-red';
        }
        //查询登陆者的比赛信息
        $my_team= $Team ->where("id=$id")->find();
        $this->assign('my_team',$my_team);

        $team_ids  = $teams[0]['team_id'].",".$teams[1]['team_id'];
        //查询本场比赛mvp
        $Data = D('Data');
        $mvp =  $Data->join("lol_team on lol_team.id = lol_data.team_id")
            ->join("lol_members on lol_members.id =lol_data.user_id")
            ->where("team_id in($team_ids)")->order("score desc")
            ->field("picture,lol_members.name username,score,result,use_hero")
            ->limit(1)->select();
        $this->assign("mvp",$mvp);
        $head_king =  $Data->join("lol_team on lol_team.id = lol_data.team_id")
            ->join("lol_members on lol_members.id =lol_data.user_id")
            ->where("team_id in($team_ids)")->order("lol_data.kill desc")
            ->field("picture,lol_members.name username,kill,result,use_hero")
            ->limit(1)->select();
        if($head_king[0]['result']=='胜'){
            $head_king[0]['color']='z-green';
        }
        else{
            $head_king[0]['color']='z-red';
        }
        $this->assign("head_king",$head_king);

        $assists =  $Data->join("lol_team on lol_team.id = lol_data.team_id")
            ->join("lol_members on lol_members.id =lol_data.user_id")
            ->where("team_id in($team_ids)")->order("lol_data.assists desc")
            ->field("picture,lol_members.name username,assists,result,use_hero")
            ->limit(1)->select();
        if($assists[0]['result']=='胜'){
            $assists[0]['color']='z-green';
        }
        else{
            $assists[0]['color']='z-red';
        }
        $this->assign("assists",$assists);
        $money =  $assists =  $Data->join("lol_team on lol_team.id = lol_data.team_id")
            ->join("lol_members on lol_members.id =lol_data.user_id")
            ->where("team_id in($team_ids)")->order("lol_data.money desc")
            ->field("picture,lol_members.name username,lol_data.money money,result,use_hero")
            ->limit(1)->select();
        if($money[0]['result']=='胜'){
            $money[0]['color']='z-green';
        }
        else{
            $money[0]['color']='z-red';
        }
        $this->assign("money",$money);
        $this->assign("teams",$teams);
        $this->display();
    }
    public function match($id){
        header("Content-type:text/html;charset=utf-8");
        $loginuser = $this->_user(true);
        $Match= D('Match');
        $Data = D('Data');
        $matchs = $Match
            ->join("lol_team on lol_team.match_id = lol_match.id")
            ->where("lol_match.id=$id")->order("result")
            ->field("lol_match.name match_name,match_date,when_long,match_type,mvp,lol_team.name team_name,
            result,big_dragon,small_dragon,horizons,date_s,lol_team.id team_id")->select();
        $matchs[0]['color']='z-green';
        $matchs[1]['color']='z-red';

        for($i=0;$i<count($matchs);$i++){
            $team_id = $matchs[$i]['team_id'];
            $matchs[$i]['kill_num']=$Data->where("team_id=$team_id")->sum("`kill`");
            $matchs[$i]['money']=round($Data->where("team_id=$team_id")->sum("`money`"),2);
            $matchs[$i]['data']=$Data->join("lol_members on lol_members.id=lol_data.user_id")->where("team_id=$team_id")->field("lol_data.*,picture")->select();
        }
        $this->assign("matchs",$matchs);

        $this->display();
    }
}